﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ChineseSchool.BLL;
using ChineseSchool.Models.Hethong;
using System.Web.UI.HtmlControls;
using System.Data;

namespace ChineseSchool.Admin
{
    public partial class QL_SlideShow : System.Web.UI.Page
    {
        public B_Image B_Image { get; set; }
        public B_Insert_Update_Delete bll { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            B_Image = new B_Image();
            bll = new B_Insert_Update_Delete();
            if (!IsPostBack)
            {
                BinGrid();
                btnAdd.Visible = true;
                btnEdit.Visible = true;
            }
        }

        protected void BinGrid()
        {
            var lstMImage = B_Image.GetAll();
            grdImage.DataSource = lstMImage;
            grdImage.DataBind();
        }

        protected void Bin_ddlPage()
        {
            var lstMPage = (new B_Page()).GetAlls();
            ddlPage.DataSource = lstMPage;
            ddlPage.DataValueField = "PageID";
            ddlPage.DataTextField = "Title";
            ddlPage.DataBind();
            ddlPage.Items.Insert(0, new ListItem("----Chọn loại danh mục----", string.Empty));
        }
        protected void Bin_ddlPageAdd()
        {
            var lstMPage = (new B_Page()).GetAlls();
            ddlPageAdd.DataSource = lstMPage;
            ddlPageAdd.DataValueField = "PageID";
            ddlPageAdd.DataTextField = "Title";
            ddlPageAdd.DataBind();
            ddlPage.Items.Insert(0, new ListItem("----Trang mặc định(default)----", string.Empty));
        }

        protected void grdImage_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grdImage.PageIndex = e.NewPageIndex;
            BinGrid();
        }

        protected void grdImage_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            var imageId = Convert.ToInt32(e.CommandArgument);
            var mImage = new M_Image();
            if (e.CommandName.Equals("Delete", StringComparison.OrdinalIgnoreCase))
            {
                try
                {
                    bll.delete("Image", "ImageID", mImage, imageId);
                    Response.Write("<script>alert('Đã được xóa!')</script>");
                }
                catch (Exception ex)
                {
                    throw new Exception("Đã có lỗi xảy ra! Vui lòng thử lại lần sau!", ex);
                }
                BinGrid();
            }
            if (e.CommandName.Equals("Edit", StringComparison.OrdinalIgnoreCase))
            {
                panUpdate.Visible = true;
                btnAdd.Visible = false;
                btnEdit.Visible = true;
                Bin_ddlPage();
                mImage = B_Image.Selectby_id(imageId);
                var pageId = Convert.ToInt32(mImage.pageID.ToString());
                if (pageId == 0)
                {
                }
                else
                {
                    ddlPage.SelectedValue = pageId.ToString();
                }
                if (mImage.isLock.ToString() == "True")
                {
                    rbYes.Checked = true;
                }
                else
                {
                    rbNo.Checked = true;
                }
                txtImageName.Text = mImage.imageName;
                txtDescription.Text = mImage.description;
                txtLink.Text = mImage.link;
                btnEdit.CommandArgument = imageId.ToString();
            }
        }

        protected void btnNew_Click(object sender, EventArgs e)
        {
            panUpdate.Visible = false;
            PanAdd.Visible = true;
            btnAdd.Visible = true;
            btnEdit.Visible = false;
            Bin_ddlPageAdd();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            PanAdd.Visible = false;
            btnEdit.Visible = true;
            var mImage = new M_Image();
            var lstImage = B_Image.ListImage;
            var pageId = 0;
            if (ddlPageAdd.SelectedValue != "")
            {
                pageId = Convert.ToInt32(ddlPageAdd.SelectedValue);
            }
            foreach (var img in lstImage)
            {
                mImage.pageID = pageId;
                mImage.isLock = true;
                mImage.albumID = 0;
                mImage.newsID = 0;
                mImage.description = img.description;
                mImage.imageName = img.imageName;
                mImage.imageUrl = img.imageUrl;
                mImage.link = img.link;
                bll.insert("Image", "ImageID", mImage);
            }
            BinGrid();
            lstImage.Clear();
            
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            panUpdate.Visible = false;
            btnEdit.Visible = true;
            var mImage = new M_Image();
            mImage.imageID = Convert.ToInt32(btnEdit.CommandArgument);
            var imageId = Convert.ToInt32(btnEdit.CommandArgument);            

            /*tải ảnh */
            var imageUrl = "";
            var file2 = fuImageUrl.PostedFile;
            if (fuImageUrl.HasFile)
            {
                if (fuImageUrl.HasFile == false && file2.ContentLength > 500000)
                {
                    lblThongbao.Text = "File nặng quá T_T";
                }
                else
                {
                    try
                    {
                        var path2 = Server.MapPath("/admin/images/Upload/" + fuImageUrl.FileName);
                        fuImageUrl.SaveAs(path2);
                        imageUrl = "/admin/images/Upload/" + fuImageUrl.FileName;
                        lblThongbao.Text = "Tải thành công";
                    }
                    catch
                    {
                        lblThongbao.Text = "Tải ảnh thất bại! vui lòng tải lại";
                    }
                }
            }
            else
            {
                mImage = B_Image.Selectby_id(imageId);
                imageUrl = mImage.imageUrl;
            }
            /*tải ảnh */
            mImage.imageName = txtImageName.Text;
            mImage.imageUrl = imageUrl;
            mImage.link = txtLink.Text;
            var pageId = 0;
            if (ddlPage.SelectedValue != "")
            {
                pageId = Convert.ToInt32(ddlPage.SelectedValue);
            }
            mImage.isLock = rbYes.Checked;
            mImage.pageID = pageId;
            mImage.description = txtDescription.Text;
            bll.update("Image", "ImageID", mImage, imageId);
            BinGrid();
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            panUpdate.Visible = false;
            PanAdd.Visible = false;
            btnAdd.Visible = true;
            btnEdit.Visible = true;
            txtImageName.Text = "";
            txtLink.Text = "";
            txtDescription.Text = "";
            BinGrid();
        }

        protected void grdImage_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {            
            BinGrid();
        }

        protected void grdImage_RowEditing(object sender, GridViewEditEventArgs e)
        {
            grdImage.EditIndex = e.NewEditIndex;
            BinGrid();
        }
    }
}